/**
 * @fileOverview The "panelbutton" plugin.
 */
VED.plugins.add( 'panelbutton', {
   requires: 'button',
   onLoad: function() {
      function clickFn( editor ) {
         var _ = this._;

         if ( _.state == VED.TRISTATE_DISABLED )
            return;

         this.createPanel( editor );

         if ( _.on ) {
            _.panel.hide();
            return;
         }

         _.panel.showBlock( this._.id, this.document.getById( this._.id ), 4 );
      }

      VED.ui.panelButton = VED.ui.button.extend({

         /**
          * Creates a panelButton class instance.
          *
          * @constructor
          */
         initialize: function( definition ) {
            // We don't want the panel definition in this object.
            var panelDefinition = definition.panel || {};
            delete definition.panel;

            this.base( definition );

            this.document = ( panelDefinition.parent && panelDefinition.parent.getDocument() ) || VED.document;

            panelDefinition.block = {
               attributes: panelDefinition.attributes
            };
            panelDefinition.toolbarRelated = true;

            this.hasArrow = true;

            this.click = clickFn;

            this._ = {
               panelDefinition: panelDefinition
            };
         },

         createPanel: function( editor ) {
            var _ = this._;

            if ( _.panel )
               return;

            var panelDefinition = this._.panelDefinition,
               panelBlockDefinition = this._.panelDefinition.block,
               panelParentElement = panelDefinition.parent || VED.document.getBody(),
               panel = this._.panel = new VED.ui.floatPanel( editor, panelParentElement, panelDefinition ),
               block = panel.addBlock( _.id, panelBlockDefinition ),
               me = this;

            panel.onShow = function() {
               if ( me.className )
                  this.element.addClass( me.className + '_panel' );

               me.setState( VED.TRISTATE_ON );

               _.on = 1;

               me.editorFocus && editor.focus();

               if ( me.onOpen )
                  me.onOpen();
            };

            panel.onHide = function( preventOnClose ) {
               if ( me.className )
                  this.element.getFirst().removeClass( me.className + '_panel' );

               me.setState( me.modes && me.modes[ editor.mode ] ? VED.TRISTATE_OFF : VED.TRISTATE_DISABLED );

               _.on = 0;

               if ( !preventOnClose && me.onClose )
                  me.onClose();
            };

            panel.onEscape = function() {
               panel.hide( 1 );
               me.document.getById( _.id ).focus();
            };

            if ( this.onBlock )
               this.onBlock( panel, block );

            block.onHide = function() {
               _.on = 0;
               me.setState( VED.TRISTATE_OFF );
            };
         }
      });

      VED.ui.panelButton.handler = {
         create: function( definition ) {
            return new VED.ui.panelButton( definition );
         }
      };

   },
   beforeInit: function( editor ) {
      editor.ui.addHandler( VED.UI_PANELBUTTON, VED.ui.panelButton.handler );
   }
});

VED.UI_PANELBUTTON = 'panelbutton';






